﻿#ifndef DYNOBJ_H
#define DYNOBJ_H
#include <string>
#include <map>
// 定义函数指针
typedef void *(*Constructor)();

class CObjectFactory
{
public:
    static void registerClass(std::string className, Constructor constructor){
        constructors()[className] = constructor;
    }

    static void *createObject(std::string className){
        Constructor constructor = nullptr;
        if(constructors().find(className) != constructors().end()){
            constructor = (Constructor)constructors().find(className)->second();
        }
        if(constructor == nullptr) return nullptr;

        return (*constructor)();
    }
private:
    // string->key:动态创建的类名， value是构建
    inline static std::map<std::string, Constructor>& constructors(){
        static std::map<std::string, Constructor> instance;
        return instance;
    }
};



#endif // DYNOBJ_H
